<template>
<div id="chat">
    <header id="header">
        <b class="back" @click="handleBack">&lt;</b>
        <h3 class="chat-user">{{chatuser.username}}</h3>
    </header>
    <div id="container" ref="container">
        <ul class="chat-list">
            <li 
                :class="[chatuser.username===item.from.username||item.from.username!==userSelf? 'list': 'list-right']"
                v-for="(item, index) in chatContentList"
                :key="index"
            >
                <img :src="item.from.headImg" alt="">
                <div :class="[chatuser.username===item.from.username||item.from.username!==userSelf? 'list-con': 'list-con-right']">
                    <p class="con-name">{{item.from.username}}</p>
                    <span class="con-info">{{item.chatInfo}}</span>
                </div>
            </li>
        </ul>
    </div>
    <footer id="footer">
        <div class="send-msg" ref="labelMsg">
            <div class="msg" contenteditable="true"></div>
            <button type="button" class="send" @click="handleBtnSendMsg">发送</button>
        </div>
    </footer>
</div>
</template>

<script>
export default {
    name: "chat",
    props: ["chatuser", "news"],
    data() {
        return {
            chatContentList: [],
            userSelf: null,
        }
    },
    mounted() {
        console.log("news：", this.news)
        this.getChatContentListOfLocalStorage()
        this.userSelf = this.$root.userSelf.username
        this.socket.emit("readNewMsg", {
            self: this.$root.userSelf.username,
            msgOfOtherUser: this.chatuser.username
        })
        this.positionToBottom()
    },
    updated() {
        this.positionToBottom()
    },
    methods: {
        // 获取本地存储的信息内容列表
        getChatContentListOfLocalStorage() {
            this.chatContentList = JSON.parse(localStorage.getItem(`chatUserMsg-${this.$root.userSelf.username}-${this.chatuser.username}`)) || []
        },
        // 保存到本地存储
        saveChatContentListToLocalStorage() {
            localStorage.setItem(`chatUserMsg-${this.$root.userSelf.username}-${this.chatuser.username}`, JSON.stringify(this.chatContentList))
        },

        // 发送消息
        handleBtnSendMsg() {
            let msgInfo = {
                from: this.$root.userSelf,
                to: this.chatuser,
                chatInfo: this.$refs.labelMsg.children[0].innerText,
                chatTime: Date.now()
            }
            console.log("发送的消息：", msgInfo)
            this.chatContentList.push(msgInfo)
            this.socket.emit("sendMsg", msgInfo)
            this.saveChatContentListToLocalStorage()
            this.$refs.labelMsg.children[0].innerText = ""
            this.$refs.labelMsg.children[1].style.marginTop = 0
            this.positionToBottom()
        },

        // 发送按钮的位置（marginTop）
        handleEnterSendMsg() {
            this.$refs.labelMsg.children[1].style.marginTop=this.$refs.labelMsg.children[1].parentNode.offsetHeight <= 40 
                                    ? 0 : this.$refs.labelMsg.children[1].parentNode.offsetHeight >= 68 
                                    ? "20px" : "6px"
        },

        // 退出聊天窗口
        handleBack() {
            this.$emit("back", false)
        },

        // 显示新消息,定位到最低端
        positionToBottom() {
            let container = this.$refs.container
            //判断元素是否出现了滚动条
            // if(container.scrollHeight > container.clientHeight) {
                //设置滚动条到最底部（延迟滚动）
                setTimeout(() => {
                    // container.scrollTop = container.scrollHeight;
                    container.scrollTop = container.scrollHeight - container.clientHeight;
                }, 100)
            // }
        }
    },
    watch: {
        // 监听最新消息
        news: function(newVal) {
            console.log(newVal);
            this.chatContentList.push(newVal)
            // this.chatContentList = [...this.chatContentList, ...newVal]
            this.saveChatContentListToLocalStorage()
        }
    }
}
</script>

<style scoped>
#chat {
    overflow: auto;
    display: flex;
    display: -webkit-flex;
    flex-direction: column;
    width: 100vw;
    height: 100vh;
}
#header,
#footer {
    position: relative;
    left: 0;
    right: 0;
    z-index: 10;
    width: 100vw;
    text-align: center;
}
#header {
    top: 0;
    height: 50px;
    line-height: 50px;
    background-color: aqua;
}
#header .back {
    position: absolute;
    top: 0;
    left: 0;
    width: 50px;
    height: 50px;
    background-color: rgb(106, 178, 247, .5);
    font-size: 24px;
    cursor: pointer;
}
#header .chat-user {
    padding: 0 50px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}
#container {
    overflow: auto;
    flex: 1;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    -moz-box-flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: text;
}
.chat-list {
    width: calc(100vw - 12px);
    height: 100vh;
}
.chat-list .list,
.chat-list .list-right {
    display: -webkit-flex;
    display: flex;
    flex-direction: row;
    margin-top: 10px;
}
.chat-list .list:last-of-type,
.chat-list .list-right:last-of-type {
    margin-bottom: 10px;
}
.chat-list .list-right {
    flex-direction: row-reverse;
}
.chat-list .list-right>img,
.chat-list .list>img {
    width: 45px;
    height: 45px;
    border-radius: 50%;
}
.chat-list .list .list-con,
.chat-list .list-right .list-con-right{
    display: -webkit-flex;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-top: 6px;
    margin-left: 12px;
}
.chat-list .list-right .list-con-right {
    align-items: flex-end;
    margin-right: 12px;
}
.chat-list .list-right .list-con-right .con-name,
.chat-list .list .list-con .con-name {
    /* display: none; */
    margin-bottom: 2px;
    font-size: 13px;
}
.chat-list .list .list-con>.con-info,
.chat-list .list-right .list-con-right>.con-info {
    display: inline-block;
    position: relative;
    max-width: calc(100vw - 140px);
    min-width: 10px;
    min-height: 22px;
    padding: 5px 8px;
    border-radius: 5px;
    font-size: 16px;
    background-color: #3b9cf7;
    word-break: break-all;
    white-space: pre-wrap;
}
.chat-list .list .list-con>.con-info::after,
.chat-list .list-right .list-con-right>.con-info::after {
    display: inline-block;
    position: absolute;
    top: 6px;
    width: 0;
    height: 0;
    border-top: 4px solid transparent;
    border-bottom: 4px solid transparent;
    content: "";
}
.chat-list .list .list-con>.con-info::after {
    left: -4px;
    border-right: 6px solid #3b9cf7;
    transform: rotateZ(90deg);
    border-bottom-right-radius: 6px;
}
.chat-list .list-right .list-con-right>.con-info::after {
    right: -4px;
    border-left: 6px solid #3b9cf7;
    border-bottom-left-radius: 20px;
    transform: rotateZ(-90deg);
}

#footer {
    bottom: 0;
    min-height: 34px;
    max-height: 100px;
    background-color: aquamarine;
}
#footer .send-msg {
    display: -webkit-flex;
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    align-items: flex-start;
    min-height: 34px;
    max-height: 100px;
    padding: 0 10px;
}
.send-msg .msg,
.send-msg .send {
    border: none;
    outline: none;
}
.send-msg .msg {
    overflow-y: scroll;
    flex: 1;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    -moz-box-flex: 1;
    min-height: 34px;
    max-height: 100px;
    padding: 0.6em 1.2em;
    margin-right: 10px;
    text-align: left;
    line-height: 17px;
    color: #000000;
    background-color: #ffffff;
    border-radius: 17px;
    font-size: 14px;
    word-break: break-all;
    word-wrap: break-word;
    box-sizing: border-box;
}
#footer .send-msg .msg div{color: #000000 !important;background-color: #ffffff !important;}
.send-msg .send {
    width: 60px;
    height: 34px;
    background-color: #3b9cf7;
    border-radius: 17px;
    cursor: pointer;
}
.send-msg .msg:empty:before{ /*设置placeholder的颜色*/
    content: attr(placeholder);
    color: #999;
}
.send-msg .msg:focus:before{
    content: none;
}
</style>